#include <iostream>
#include <fstream>
#include "picojson.h"

using namespace std;

class PropInterface {
public :
    PropInterface(picojson::object& prop) :  _prop(&prop) {}
    PropInterface(picojson::value& prop)  :  _prop(&prop.get<picojson::object>()) {}

    void typeID(int id){ (*_prop)["typeID"] = picojson::value((float)id); }

    const string& type()    { return getString("type"); }
    const string& comment() { return getString("comment"); }
    double value()   { return getNumber("value"); }
    int typeID()   { return getInt("typeID"); }

public :
    int     getInt(const string& name)      { return (int)getNumber(name); }
    float   getFloat(const string& name)    { return (float)getNumber(name); }
    double  getNumber(const string& name)   { return (*_prop)[name].get<double>(); }
    const   string& getString(const string& name) { return (*_prop)[name].get<string>(); }

private :
    picojson::object* _prop;
};

void settingProperty(PropInterface pif) {
    switch (pif.typeID()) {
        case 0 :
            {
                cout << "aaaaaaaaaaaaaa" << endl;
                break;
            }
    }
}



class PropManager {
public :
    void initialize(picojson::value& json)
    {
        picojson::value& type_value = json.get<picojson::object>()["type"];
        _typeList = &type_value.get<picojson::object>();

        picojson::value& prop_value = json.get<picojson::object>()["prop"];
        _propList = &prop_value.get<picojson::object>();

        picojson::value& part_value = json.get<picojson::object>()["part"];
        _partList = &part_value.get<picojson::object>();

        //cout << prop_value << endl;
        //cout << part_value << endl;
    }
    void print(const std::string& type)
    {
        using namespace picojson;
        value&  target_part_value = (*_partList)[type];
        object& target_part_object= target_part_value.get<object>();

        array& part_prop_array = target_part_object["prop"].get<picojson::array>();
        cout << type << endl;
        for (array::iterator iter = part_prop_array.begin(); iter != part_prop_array.end(); ++iter) {
            cout << "    " << (*iter).get<string>() << endl;

            std::string& prop_name = (*iter).get<string>();
            PropInterface pif( (*_propList)[prop_name] );
            pif.typeID( (*_typeList)[pif.type()].get<double>() );
            cout << "        "
                << pif.type() << ", "
                << pif.comment() << ", "
                << pif.typeID() << ", "
                << pif.value() << endl;

            settingProperty(pif);
        }
    }
private :
    picojson::object* _typeList;
    picojson::object* _propList;
    picojson::object* _partList;
};

int main()
{
    ifstream ifs("part-property.json");
    picojson::value v;
    ifs >> v;

    PropManager pm;
    pm.initialize(v);
    pm.print("Text");

    system("pause");

    return 0;
}